/**
 * 所属节点：盘点单创建（全仓）
 * 创建人：zm
 * 创建时间：2024年8月27日
 * 最后修改人：zm
 */
cb.define(process.env.__DOMAINKEY__, function () {
  let GDGY01_dxq_checkstock_createAll_VM_Extend = {
    doAction: function (name, viewModel) {
      if (this[name]) this[name](viewModel);
    },
    init: function (viewModel) {
      const qianzhui = "/gdgy/pc/isv2";
      let userInfo = viewModel.getAppContext().user;
      let tenantID = viewModel.getAppContext().tenant.tenantId;
      viewModel.get("warehouseId_name") &&
        viewModel.get("warehouseId_name").on("afterValueChange", function (data) {
          // 仓库--值改变后
          // console.log(data);
          var warehousename = data.obj.value;
          viewModel.get("warehousename").setValue(warehousename);
        });

      viewModel.get("warehousename") &&
        viewModel.get("warehousename").on("afterValueChange", function (data) {
          // 仓库名称--值改变后
        });

      viewModel.on("customInit", function (data) {
        var timestr = new Date().format("yyyy-MM-dd hh:mm:ss");
        var locationArr = [];
        var warehouseId_name = viewModel.get("warehouseId_name");
        var btnAdd = viewModel.get("btnSaveAndAdd"); //保存并新增
        var btnAddSave = viewModel.get("btnSave"); //保存

        warehouseId_name.on("afterValueChange", function (data) {
          locationArr = [];
          var warehouseId = viewModel.get("warehouseId").getValue();

          // // 仓库--值改变后
          // var arrResult = cb.rest.invokeFunction(
          //   "GDGY01.Location.getlocationData",
          //   { warehouseID: warehouseId, sourceType: 1 },
          //   function (err, res) {},
          //   viewModel,
          //   { async: false }
          // );

          // // console.log(arrResult);
          // const locationArrRes = arrResult.result.rst;

          /**
           * 张明迁移
           */
          // 仓库--值改变后
          let url_lodata =
            qianzhui +
            "/location/getlocationList?tenant_id=" +
            tenantID +
            "&warehouseId=" +
            warehouseId +
            "&level=" +
            1;
          const proxy_lodata = viewModel.setProxy({
            lodata: {
              url: url_lodata,
              method: "GET",
              options: {
                mask: false,
              },
            },
          });
          const locationArrRes = proxy_lodata.lodataSync({});
          for (var i = 0; i < locationArrRes.dataList.length; i++) {
            var sonOrder = {};
            sonOrder.locationCode = locationArrRes.dataList[i].locationCode;
            sonOrder.locationID = locationArrRes.dataList[i].locationID;
            sonOrder.locationName = locationArrRes.dataList[i].locationName;
            sonOrder.warehouseName = locationArrRes.dataList[i].warehouseName;
            sonOrder.warehouseCode = locationArrRes.dataList[i].warehouseCode;
            sonOrder.createTime = timestr;
            locationArr.push(sonOrder);
          }
        });

        btnAddSave.on("click", function (data) {
          //保存
          // 保存--单击
          let yearStr = new Date().getFullYear();
          let monthStr = new Date().getMonth() + 1;
          let dateStr = new Date().getDate();
          let hourStr = new Date().getHours();
          let minuteStr = new Date().getMinutes();
          let secondStr = new Date().getSeconds();

          if (monthStr < 10) {
            monthStr = "0" + String(monthStr);
          } else {
            monthStr = String(monthStr);
          }

          if (dateStr < 10) {
            dateStr = "0" + String(dateStr);
          } else {
            dateStr = String(dateStr);
          }

          if (hourStr < 10) {
            hourStr = "0" + String(hourStr);
          } else {
            hourStr = String(hourStr);
          }

          if (minuteStr < 10) {
            minuteStr = "0" + String(minuteStr);
          } else {
            minuteStr = String(minuteStr);
          }

          if (secondStr < 10) {
            secondStr = "0" + String(secondStr);
          } else {
            secondStr = String(secondStr);
          }
          let suijiCode =
            String(yearStr) + monthStr + String(dateStr) + String(hourStr) + String(minuteStr) + String(secondStr);
          var warehouseId = viewModel.get("warehouseId").getValue();
          var warehousename = viewModel.get("warehouseId_name").getValue();
          var checkType =
            viewModel.get("checkType").getValue() === undefined || viewModel.get("checkType").getValue() === null
              ? 1
              : viewModel.get("checkType").getValue();
          var remark = viewModel.get("cRemark").getValue() === undefined ? "" : viewModel.get("cRemark").getValue();

          // cb.rest.invokeFunction(
          //   "GDGY01.pandian.Creattakestock",
          //   {
          //     billNum: "dxq_checkstock_createAll",
          //     warehouseId: warehouseId,
          //     sourceType: 0,
          //     checkType: checkType,
          //     warehouseName: warehousename,
          //     Codesuiji: suijiCode,
          //     locationarr: locationArr,
          //     cRemark: remark,
          //   },
          //   function (err, res) {
          //     // console.log(res);
          //     // console.log(err);
          //     if (res !== null && res !== undefined && err === null) {
          //       // console.log(err);
          //       // console.log(res);
          //       cb.utils.alert("成功生成盘点单！");
          //       var warehouse = viewModel.get("warehouseId_name");
          //       warehouse.setDisabled(true);
          //       viewModel.clear();

          //       let data1 = {
          //         billtype: "VoucherList", // 单据类型
          //         billno: "dxq_checkstockList", // 单据号
          //         params: {
          //           mode: "browse", // (编辑态、新增态、浏览态)
          //         },
          //       };
          //       cb.loader.runCommandLine("bill", data1, viewModel);
          //     }
          //   }
          // );

          /**
           * 张明迁移
           */
          const url_Creattakestockall =
            qianzhui + "/checkstock/CreatCheckStock?tenant_id=" + tenantID + "&userId=" + userInfo.userId;
          const proxy_Creattakestockall = viewModel.setProxy({
            Creattakestockall: {
              url: url_Creattakestockall,
              method: "POST",
            },
          });
          proxy_Creattakestockall.Creattakestockall(
            {
              billNum: "dxq_checkstock_createAll",
              warehouseId: warehouseId,
              sourceType: 0,
              checkType: checkType,
              warehouseName: warehousename,
              Codesuiji: suijiCode,
              locationarr: locationArr,
              cRemark: remark,
            },
            function (err, res) {
              if (res !== null && res !== undefined && err === null) {
                // console.log(err);
                // console.log(res);
                cb.utils.alert("成功生成盘点单！");
                var warehouse = viewModel.get("warehouseId_name");
                warehouse.setDisabled(true);
                viewModel.clear();
                let data1 = {
                  billtype: "VoucherList", // 单据类型
                  billno: "dxq_checkstockList", // 单据号
                  params: {
                    mode: "browse", // (编辑态、新增态、浏览态)
                  },
                };
                cb.loader.runCommandLine("bill", data1, viewModel);
              }
            }
          );
        });

        btnAdd.on("click", function (data) {
          //保存并新增
          // 保存并新增--单击
          let yearStr = new Date().getFullYear();
          let monthStr = new Date().getMonth() + 1;
          let dateStr = new Date().getDate();
          let hourStr = new Date().getHours();
          let minuteStr = new Date().getMinutes();
          let secondStr = new Date().getSeconds();

          if (monthStr < 10) {
            monthStr = "0" + String(monthStr);
          } else {
            monthStr = String(monthStr);
          }

          if (dateStr < 10) {
            dateStr = "0" + String(dateStr);
          } else {
            dateStr = String(dateStr);
          }

          if (hourStr < 10) {
            hourStr = "0" + String(hourStr);
          } else {
            hourStr = String(hourStr);
          }

          if (minuteStr < 10) {
            minuteStr = "0" + String(minuteStr);
          } else {
            minuteStr = String(minuteStr);
          }

          if (secondStr < 10) {
            secondStr = "0" + String(secondStr);
          } else {
            secondStr = String(secondStr);
          }
          let suijiCode =
            String(yearStr) + monthStr + String(dateStr) + String(hourStr) + String(minuteStr) + String(secondStr);
          var warehouseId = viewModel.get("warehouseId").getValue();
          var warehousename = viewModel.get("warehouseId_name").getValue();
          var checkType =
            viewModel.get("checkType").getValue() === undefined || viewModel.get("checkType").getValue() === null
              ? 1
              : viewModel.get("checkType").getValue();
          var remark = viewModel.get("cRemark").getValue() === undefined ? "" : viewModel.get("cRemark").getValue();

          // cb.rest.invokeFunction(
          //   "GDGY01.pandian.Creattakestock",
          //   {
          //     billNum: "dxq_checkstock_createAll",
          //     warehouseId: warehouseId,
          //     sourceType: 0,
          //     checkType: checkType,
          //     warehouseName: warehousename,
          //     Codesuiji: suijiCode,
          //     locationarr: locationArr,
          //     cRemark: remark,
          //   },
          //   function (err, res) {
          //     // console.log(res);
          //     // console.log(err);
          //     if (res !== null && res !== undefined && err === null) {
          //       cb.utils.alert("成功生成盘点单！");
          //       var warehouse = viewModel.get("warehouseId_name");
          //       warehouse.setDisabled(true);
          //       viewModel.clear();
          //     }
          //   }
          // );

          /**
           * 张明迁移
           */
          const url_Creattakestockall =
            qianzhui + "/checkstock/CreatCheckStock?tenant_id=" + tenantID + "&userId=" + userInfo.userId;
          const proxy_Creattakestockall = viewModel.setProxy({
            Creattakestockall: {
              url: url_Creattakestockall,
              method: "POST",
            },
          });
          proxy_Creattakestockall.Creattakestockall(
            {
              billNum: "dxq_checkstock_createAll",
              warehouseId: warehouseId,
              sourceType: 0,
              checkType: checkType,
              warehouseName: warehousename,
              Codesuiji: suijiCode,
              locationarr: locationArr,
              cRemark: remark,
            },
            function (err, res) {
              if (res !== null && res !== undefined && err === null) {
                cb.utils.alert("成功生成盘点单！");
                var warehouse = viewModel.get("warehouseId_name");
                warehouse.setDisabled(true);
                viewModel.clear();
              }
            }
          );
        });
      });

      viewModel.get("btnSaveAndAdd") &&
        viewModel.get("btnSaveAndAdd").on("click", function (data) {
          // 保存并新增--单击
        });

      viewModel.get("btnSave") &&
        viewModel.get("btnSave").on("customInit", function (data) {
          // 保存--单击
        });
    },
  };
  try {
    module.exports = GDGY01_dxq_checkstock_createAll_VM_Extend;
  } catch (error) {}
  return GDGY01_dxq_checkstock_createAll_VM_Extend;
});
